#!/bin/bash

# Using the -c option will copy zephir in /usr/local/bin
options='c'
while getopts $options option
do
  if [[ "$option" == "c" ]]; then
    # Use ZEPHIRDIR env variable if defined as installation destination dir
    ZEPHIRSRC="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    if [[ -z $ZEPHIRDIR ]]; then
      ZEPHIRDIR=$ZEPHIRSRC
    elif [[ ! -d $ZEPHIRDIR ]]; then
      echo "Directory defined by environment variable ZEPHIRDIR doesn't exist"
      exit 1
    fi
    sed "s#%ZEPHIRDIR%#$ZEPHIRDIR#g" bin/zephir > bin/zephir-cmd
    chmod 755 bin/zephir-cmd
    sudo cp bin/zephir-cmd /usr/local/bin/zephir
    rm bin/zephir-cmd
    if [[ $ZEPHIRDIR != $ZEPHIRSRC ]]; then
        sudo cp -r $ZEPHIRSRC/* $ZEPHIRDIR
        echo "Zephir was installed correctly"
    fi
    exit 0
  fi
done

if [[ -d "/etc/bash_completion.d/" && $EUID = 0 ]]; then
  sudo cp bin/bash_completion /etc/bash_completion.d/zephir
fi
